package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lx
 * @Time: 15:54
 * @Description:  后台管理中心
 *
 *
 */
@RestController
//@CrossOrigin
@RequestMapping("admin/product/")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    //查询一级分类
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> list = managerService.getCategory1();
        return Result.ok(list);
    }
    //查询 二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id){
        List<BaseCategory2> list = managerService.getCategory2(category1Id);
        return Result.ok(list);
    }
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id){

        List<BaseCategory3> list = managerService.getCategory3(category2Id);
        return Result.ok(list);
    }
    //根据一二三级分类ID 查询 平台属性集合
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable(name = "category1Id") Long category1Id,
            @PathVariable(name = "category2Id") Long category2Id,
            @PathVariable(name = "category3Id") Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(
                category1Id,category2Id,category3Id
        );
        return Result.ok(baseAttrInfoList);
    }

    //平台属性保存
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
    //查询所有品牌 要求分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit
    ){
        IPage iPage = managerService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //查询品牌集合
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //查询销售属性表
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    //保存SPU数据
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        //spuInfo对象
           //spuImage对象 集合
           //spuSaleAttr 集合
                //SpuSaleAttrValue 集合
        managerService.saveSpuInfo(spuInfo);


        return Result.ok();
    }

    //查询分页结果  SPU商品信息
    @GetMapping("/{page}/{limit}")
    public Result spuInfoPage(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit,
            Long category3Id
            ){
        IPage<SpuInfo> iPage = managerService.spuInfoPage(page,limit,category3Id);
        return Result.ok(iPage);
    }
    //根据spuId 查询所有图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据spuId查询销售属性及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //保存SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        //Sku信息表
          //图片表  集合
          //平台属性表 集合
          //销售属性表 集合
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    //查询所有SKuINfo的数据
    @GetMapping("/list/{page}/{limit}")
    public Result list(
            @PathVariable(name = "page") Long page,
            @PathVariable(name = "limit") Long limit
    ){
        IPage<SkuInfo> iPage = managerService.list(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId){
        managerService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId") Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
    @PostMapping("baseTrademark/save")
    public Result save(@RequestBody BaseTrademark baseTrademark){
        managerService.save(baseTrademark);
        return Result.ok();
    }
    @PutMapping("baseTrademark/update")
    public Result update(@RequestBody BaseTrademark baseTrademark){
        managerService.update(baseTrademark);
        return Result.ok();
    }
    @GetMapping("baseTrademark/get/{id}")
    public Result edit(@PathVariable Long id){
        BaseTrademark baseTrademark = managerService.edit(id);
        return Result.ok(baseTrademark);
    }
}
